package com.puti.doctor.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.puti.doctor.domian.GoodsMarketing;
import com.puti.doctor.service.IGoodsMarketingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.puti.common.log.annotation.Log;
import com.puti.common.log.enums.BusinessType;
import com.puti.common.security.annotation.PreAuthorize;

import com.puti.common.core.web.controller.BaseController;
import com.puti.common.core.web.domain.AjaxResult;
import com.puti.common.core.utils.poi.ExcelUtil;
import com.puti.common.core.web.page.TableDataInfo;

/**
 * 商品营销 商品营销Controller
 *
 * @author puti
 * @date 2021-04-21
 */
@CrossOrigin
@RestController
@RequestMapping("/marketing")
public class GoodsMarketingController extends BaseController
{
    @Autowired
    private IGoodsMarketingService goodsMarketingService;

    /**
     * 查询商品营销 商品营销列表
     */
    @PreAuthorize(hasPermi = "system:marketing:list")
    @GetMapping("/list")
    public TableDataInfo list(GoodsMarketing goodsMarketing)
    {
        startPage();
        List<GoodsMarketing> list = goodsMarketingService.selectGoodsMarketingList(goodsMarketing);
        return getDataTable(list);
    }
    //秒杀时间
    @GetMapping("/listset")
    public TableDataInfo lists(GoodsMarketing goodsMarketing) throws ParseException {
        startPage();
        List<GoodsMarketing> list = goodsMarketingService.selectGoodsMarketingList(goodsMarketing);
        for (GoodsMarketing marketing : list) {

            Date marketingStarttime = marketing.getMarketingStarttime();
            Date marketingEndtime = marketing.getMarketingEndtime();
            long time = marketingStarttime.getTime();
            long time1 = marketingEndtime.getTime();
            if (time<time1){
                System.out.println("秒杀时间已结束");
            }
        }
        return getDataTable(list);
    }


    /**
     * 导出商品营销 商品营销列表
     */
    @Log(title = "商品营销 商品营销", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GoodsMarketing goodsMarketing) throws IOException
    {
        List<GoodsMarketing> list = goodsMarketingService.selectGoodsMarketingList(goodsMarketing);
        ExcelUtil<GoodsMarketing> util = new ExcelUtil<GoodsMarketing>(GoodsMarketing.class);
        util.exportExcel(response, list, "商品营销 商品营销数据");
    }

    /**
     * 获取商品营销 商品营销详细信息
     */
    @GetMapping(value = "/{marketingId}")
    public AjaxResult getInfo(@PathVariable("marketingId") Long marketingId)
    {
        return AjaxResult.success(goodsMarketingService.selectGoodsMarketingById(marketingId));
    }

    /**
     * 新增商品营销 商品营销
     */
    @Log(title = "商品营销 商品营销", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GoodsMarketing goodsMarketing)
    {
        return toAjax(goodsMarketingService.insertGoodsMarketing(goodsMarketing));
    }

    /**
     * 修改商品营销 商品营销
     */
    @Log(title = "商品营销 商品营销", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GoodsMarketing goodsMarketing)
    {
        return toAjax(goodsMarketingService.updateGoodsMarketing(goodsMarketing));
    }

    /**
     * 删除商品营销 商品营销
     */
    @Log(title = "商品营销 商品营销", businessType = BusinessType.DELETE)
	@DeleteMapping("/{marketingIds}")
    public AjaxResult remove(@PathVariable Long[] marketingIds)
    {
        return toAjax(goodsMarketingService.deleteGoodsMarketingByIds(marketingIds));
    }


    /**
     *秒杀专场：商品列表：按照时间正序排列；
     */
    @GetMapping("/puti-marketing")
    public List<GoodsMarketing> selectMarketing(GoodsMarketing goodsMarketing){
        return goodsMarketingService.selectMarketing(goodsMarketing);
    }

    /**
     *秒杀专场：获取后台秒杀商品数据，展示上架商品信息；
     */
    @GetMapping("/puti-goodsMarketing")
    public List<GoodsMarketing> selectGoodsMarketing(GoodsMarketing goodsMarketing){
        return goodsMarketingService.selectGoodsMarketing(goodsMarketing);
    }
}
